package org.itboys.weixin.tools;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.itboys.commons.CommonConstants;
import org.itboys.core.util.*;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * 作者：zzq
 * 时间：2018/6/29
 * 描述：
 */
public class ModelMessageUtil {
    public static void sendModelMessage(String fuwuOpenId, String nickName, String time, BigDecimal score,String type,BigDecimal currentScore) throws IOException {
        String access_token = WeixinUtils.getAccessToken("wx5396325b6b627d7a", "2677a571c214529573e36dd36aaa7206");
        //SmallCodeUtil.getminiqrQr("1",accessToken);
        JSONObject object=new JSONObject();
        JSONObject data =new JSONObject();
        object.put("touser","otD_Gw-pm9vvBd9-Z7WifYv3Non8");
        object.put("template_id","yr7r7_qwPMKf-KZQxtaqYVmTZAujsKbYlcH8FzWTsds");
        object.put("url","https://summer.51tjs.cn/sunday/weixin/safe/oAuth?type=7");
        JSONObject firstValue =new JSONObject();
        firstValue.put("value","账户变动提醒");
        data.put("first",firstValue);
        JSONObject keyword1 =new JSONObject();
        keyword1.put("value",nickName);
        keyword1.put("color","#173177");
        data.put("keyword1",keyword1);

        JSONObject keyword2 =new JSONObject();
        keyword2.put("value",time);
        keyword2.put("color","#173177");
        data.put("keyword2",keyword2);
        JSONObject keyword3 =new JSONObject();
        keyword3.put("value",score);
        keyword3.put("color","#173177");
        data.put("keyword3",keyword3);
        JSONObject keyword4 =new JSONObject();
        keyword4.put("value",type);
        keyword4.put("color","#173177");
        data.put("keyword4",keyword4);
        JSONObject keyword5 =new JSONObject();
        keyword5.put("value",currentScore);
        keyword5.put("color","#173177");
        data.put("keyword5",keyword5);
        JSONObject remark =new JSONObject();
        remark.put("value","详情点击查看");
        remark.put("color","#173177");
        data.put("remark",remark);

        object.put("data",data);
        String messageUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
        PostMethod postMethod = new PostMethod(messageUrl);
        HttpClient httpclient = org.itboys.core.util.HttpHelper.getHttpClient();
        RequestEntity requestEntity = null;
        //JSONObject jsonObject=map2Json(param);
        String result = object.toJSONString();
        System.out.println(result);
        requestEntity = new StringRequestEntity(result, CommonConstants.CONTENT_TYPE.JSON_TYPE , CommonConstants.ENCODE.UTF_8);
        postMethod.setRequestEntity(requestEntity);
        httpclient.executeMethod(postMethod);
        String createResult = postMethod.getResponseBodyAsString();
    }
    public static void main(String[] a){
        //String access_token=WeixinUtils.getAccessToken("wxa6b6512f943b34ef", "65e8ea68b0333f960e64d4aefdefd89b");

    }
}
